#!/usr/bin/php
<?php
require_once("../kernel/init.php");
if( $Gepard->runmode() != _CONSOLE_RUNMODE ){
	print "Error. Can run only in cli mode\n";
	exit;
}
/* Setting up install object */
$lang = file_get_contents("lang/en");
$lang = $Gepard->lang->parse($lang);
require_once("install.object.php");
$install = new install($Gepard,$lang);
$Gepard->set("messageonerror",false);
$conf = $install->checkglobalconsole();
if( is_string($conf)){
	print "Gepard reports some errors in your system configuration\n\n";
	print str_replace("<br>","\n",$conf);
	exit;
}
function write($message){
	print ($message."\n");
}
function abort(){
	print "\nAborted\n\n"; exit;
}
function writetitle( $message ){
	$message = "\n------------ $message ------------\n\n";
	print ($message);
}
function confirm ($question ){
	global $Gepard;
	$message=$question." [Y]  ";
	$answ = $Gepard->console->read($message);
	print "\n\n";
	if( strtolower($answ) != "y"){
	return false;
	} else return true;
}
writetitle("Welcome to Gepard installation system");
write("System configuration: Gepard Version = ".$Gepard->getversion().", Operating system = ".PHP_OS.", PHP Version = ".PHP_VERSION);
if( !confirm("This software is under GNU General Public License, did you agree with it's terms of use?") ) abort();
write("System core files path is ".$Gepard->path());
/* Getting information about site */
$sysname = $Gepard->console->read("Enter your web application name (optional, default is app0): ");
$sysname = strlen($sysname)!=0?$sysname:"app0";
$sysurl  = $Gepard->console->read("Enter your web application url: ");
if( strlen($sysurl)==0) {
abort();
}
function getlanguage(){
	global $Gepard;
	write("Please, choose your system default language");
	$languages = $Gepard->syslanguages();
	for($i=1;$i<=count($languages);$i++){
	write("\t".$i.".  ".$languages[$i-1]);
	}
	$language = $Gepard->console->read("Enter a number: ");
	if( is_numeric($language) && (int)$language>=1 && (int)$language<=count($languages)){
		return $languages[$language-1];
	} return false;
}
$language = false;
while( $language==false){
	$language=getlanguage();
}
/*
 * Collection administrator data
 */ 
function adminemail(){
	global $Gepard;
	$email = $Gepard->console->read("Please, enter your administration email (use it as login): ");
	if( preg_match("/^([A-z0-9_.-]+)\@([A-z0-9_.-]+)$/",$email))
	return $email;
	write("Email is incorrect, try again");
	return false;
}
function adminpasswd(){
	global $Gepard;
	$passwd = $Gepard->console->read("Please, enter you administrator password: ");
	if( strlen($passwd) < 5 ){
		write("Password is too short, minimum 5 symbols, try again");
		return false;
	}
	return $passwd;
}
$adminemail = false;
while( !$adminemail) {
	$adminemail = adminemail();
}
$adminpasswd = false;
while( !$adminpasswd) {
	$adminpasswd = adminpasswd();
}
/* Collecting information about database */
$dbdrv = null;
$dbserv = null;
$dbuser = null;
$dbpasswd = null;
$dbdb = null;
$usedbo = confirm("Would you like to set database parameters? ");
function dboserv(){
	global $Gepard;
	$dbos = array("mysql","mysqli","pgsql","mssql","ibase","sqlite");	
	write ("Please, select your database engine:\n");
	for($i=1;$i<=count($dbos);$i++){
		print "\t$i. ".$dbos[$i-1]."\n";
	}
	$s = $Gepard->console->read("Enter a number: ");
	if( is_numeric($s) && (int)$s>=1 && (int)$s<=count($dbos)){
		return $dbos[(int)$s-1];
	}
	return false;
}
function dboconf( ){
	global $dbdrv,$dbserv,$dbuser,$dbpasswd,$dbdb,$Gepard,$install;
	$dbdrv = false;
	while( !$dbdrv ){
		$dbdrv = dboserv();
	}
	$dbserv = $Gepard->console->read("Please, enter your database server: ");	
	$dbdb = $Gepard->console->read("Please, enter your database name on server: ");	
	$dbuser = $Gepard->console->read("Please, enter your database user: ");	
	$dbpasswd = $Gepard->console->read("Please, enter your database password: ");	
	print "Trying to connect to the database... ";
	$c = array("server"=>$dbserv,"db"=>$dbdb,"user"=>$dbuser,"password"=>base64_encode($dbpasswd));
	
	$Gepard->set("exceptiononerror",true);
	try {
		$Gepard->dbo->load($dbdrv,$c);
		$Gepard->dbo->disconnect($dbdrv);
	} catch ( GepardException $e ){
		print "Error, please, re-input data or check server parameters\n";
		return false;
	}
	print "Ok\n";
	return true;
	
}
if( $usedbo) {
	$dboconf = false;
	while( !$dboconf) { $dboconf=dboconf(); $Gepard->set("exceptiononerror",false); }
}
$install->checkconf($dbdrv,$dbserv,$dbdb,$dbuser,$dbpasswd,$sysname,$language,$sysurl,$adminemail,$adminpasswd);
if( $usedbo){
	print "Installing main sql tables...";
	if( !$install->sqlinstall() ) { print "Error\n"; }
	else print "Ok\n";
}
print "Adding user account...Ok\n";
print "Writing information to configuration files...Ok\n";
$install->writefiles();
writetitle("Gepard system was successfully installed, enjoy it! (c) Taras Kudla, 2009");
?>
